package com.jrx.anytxn.transaction.job.marketingFile.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.MarketingFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Service
public class GenerateMarketingFileListener implements JobExecutionListener {
    private static final Logger logger = LoggerFactory.getLogger(GenerateMarketingFileListener.class);

    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private MarketingFileConfig marketingFileConfig;

    @Override
    public void beforeJob(JobExecution jobExecution) {
        String reportingPath = marketingFileConfig.getMarketingFilePath();
        String fileName = marketingFileConfig.getMarketingFileName();

        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        String toDay = DateUtils.format(org.getToday(),"yyyyMMdd");

        String marketSumFileName = TransBizConstant.FILE_NAME_MARKET_SUM + "_" + toDay + "_" + fileName + ".del";
        String marketDetFileName = TransBizConstant.FILE_NAME_MARKET_DET + "_" + toDay + "_" + fileName + ".del";

        String filePath = reportingPath + org.getTenantId() + File.separator + toDay + File.separator;
        Path pFile = Paths.get(filePath);

        List<Path> reportingFileList = new ArrayList<>();
        reportingFileList.add(Paths.get(filePath + marketSumFileName));
        reportingFileList.add(Paths.get(filePath + marketDetFileName));


        //判断文件夹是否存在
        if (!Files.isDirectory(pFile)) {
            //创建文件夹
            try {
                Files.createDirectories(pFile);
            } catch (IOException e) {
                logger.error("创建目录：{},失败：{}", pFile, e);
            }
        }
        // 文件已存在，先删后增
        for (Path reportingFile : reportingFileList) {
            if (Files.exists(reportingFile)) {
                try {
                    Files.delete(reportingFile);
                } catch (IOException e) {
                    logger.error("删除文件：{}，失败：{}", reportingFile, e);
                }
            }
            //创建文件
            try {
                Files.createFile(reportingFile);
            } catch (IOException e) {
                logger.error("创建文件：{}，失败：{}", reportingFile, e);
            }
        }
    }

    @Override
    public void afterJob(JobExecution jobExecution) {

    }
}
